package com.tjwx.service.impl;

import com.tjwx.dao.UserMapper;
import com.tjwx.model.JsonModel;
import com.tjwx.model.ReturnCode;
import com.tjwx.service.RegisterService;
import com.tjwx.util.DateFormatUtil;
import com.tjwx.util.MD5Util;
import com.tjwx.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("registerService")
public class RegisterServiceImpl implements RegisterService {
    Logger log = LoggerFactory.getLogger(RegisterServiceImpl.class);
    @Autowired
    UserMapper userMapper;

    @Override
    public JsonModel addUser(Map user) {
        try{
            String password = user.get("password")+"";
            String username = user.get("username")+"";
            String nowtime = DateFormatUtil.DateFormat();
            user.put("userid", StringUtil.getUUID());
            user.put("createtime", nowtime);
            user.put("updatetime", nowtime);
            user.put("userstate", 1);
            user.put("password", MD5Util.md5(password,username));
            user.put("usertype", "普通用户");
            user.put("userlevel", 1);
            userMapper.addUser(user);


            JsonModel json = new JsonModel(true, ReturnCode.SUCESS_CODE_0000.getValue(), ReturnCode.SUCESS_CODE_0000.getKey(), null);
            return json;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            JsonModel json = new JsonModel(false, ReturnCode.ERROR_CODE_11001.getValue(), ReturnCode.ERROR_CODE_11001.getKey(), null);
            return json;
        }
    }
}
